package 队列.循环队列;

import 队列.数组队列.Queue;
/*浪费一个空间但不用size*/
public class LoopQueueTwo<E> implements Queue<E> {

    private E []data;
    private int front;
    private int tail;


    public LoopQueueTwo(int capacity){
        data = (E[]) new Object[capacity+1];
        front = 0;
        tail = 0;
    }
    public LoopQueueTwo(){
        this(10);
    }

    @Override
    public void enqueue(E e) {
        if (isFull()){
            resize(2 * this.getCapacity());
        }
        data[tail] = e;
        tail = (tail+1) % data.length;
    }

    private void resize(int length) {
        E[] newData = (E[]) new Object[length + 1];
        int res = getSize();
//        System.out.println(res);
        for (int i = 0; i < getSize(); i++) {
            newData[i] = data[(front+i) % data.length];
        }
        data = newData;
        front = 0;
//        System.out.println(getSize());
        tail = res;

    }

    @Override
    public E dequeue() {
        if (isEmpty()){
            throw new IllegalArgumentException("error");
        }
        E res = data[front];
        data[front] = null;
        front = (front+1) % data.length;
        if (getSize() == getCapacity()/ 4 && getCapacity() / 2 != 0){
            resize(getCapacity() / 2);
        }
        return res;
    }

    @Override
    public E getFront() {
        if (isEmpty()){
            throw new IllegalArgumentException("error");
        }
        return data[front];
    }

    @Override
    public int getSize() {
        if (tail >= front){
            return tail - front;
        }
        else {
            /*空元素个数是front - tail， data - (front - tail)*/
            return tail - front + data.length;
        }
    }

    @Override
    public boolean isEmpty() {
        return front == tail;
    }
    public int getCapacity(){
        return data.length-1;
    }
    public boolean isFull(){
        return (tail+1) % data.length == front;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("queue size = %d, capacity = %d\n", getSize(), getCapacity()));
        builder.append("front [");
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            builder.append(data[i]);
            if ((i + 1) % data.length != tail){
                builder.append(",");
            }
        }
        builder.append("] tail");
        return builder.toString();
    }

    public static void main(String[] args) {
        LoopQueueTwo<Integer> classArray = new LoopQueueTwo<>();
        for (int i = 1; i <= 3; i++) {
            classArray.enqueue(i);
            System.out.println(classArray);
        }
        System.out.println(classArray);
        classArray.dequeue();
        System.out.println(classArray);
        classArray.dequeue();
        System.out.println(classArray);
        classArray.dequeue();
        System.out.println(classArray);
        classArray.enqueue(4);
        System.out.println(classArray);
        classArray.enqueue(5);
        System.out.println(classArray);
        classArray.enqueue(6);
        System.out.println(classArray);
    }

}
